<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/4
 * Time: 19:20
 */

namespace app\api\Service;

use app\common\model\WalletRecord;
use app\common\model\Wallet;
use app\common\model\User;
use app\common\model\WalletLog;
use app\common\model\Commission;

class AgentService
{
    public function updateMoneyToAgent($username,$agent_id,$money)
    {

        $agent = User::with('agent')->where('id',$agent_id)->field('id,pid')->find();
        $now = time();
        $agentMoney = $money * $agent['agent']['rebateNum'];
        $wallet = Wallet::where('user_id',$agent_id)->find();
        $wallet->money = $wallet['money'] + $agentMoney;
        $wallet->save();

        $recordSn = checkRecordSn();

        Commission::insert([
            'agent_id'  => $agent_id,
            'money'     => $agentMoney,
            'username'  => $username,
            'user_bet'  => $money,
        ]);


        WalletRecord::insert([  //记录钱包记录
            'record_sn' => $recordSn,
            'user_id'   => $agent_id,
            'type'      => 6,//分红
            'money'     => $agentMoney,
            'remark'    => $username.':下注'.$money.'元分红:'.$agentMoney.'元',
            'pay_status'=> 1,//支付成功
            'pay_time'  => $now,
            'createtime'=> $now,
        ]);
        WalletLog::insert([//钱包日志
            'record_sn'     => $recordSn,
            'user_id'       => $agent_id,
            'change_money'  => '+' . $agentMoney,
            'money'         => $wallet->money,//变动后的金额
            'remark'        => $username.':下注'.$money.'元分红:'.$agentMoney.'元',
            'createtime'    => $now,
        ]);
    }
}